package com.example.demo.client.view;

import com.example.demo.client.common.CommonRequest;
import com.example.demo.client.common.ViewKey;
import com.example.demo.ds.MyHashMap;
import com.example.demo.ds.MyList;
import com.example.demo.server.common.*;
import com.example.demo.server.controller.ArticleController;
import com.example.demo.server.model.ArticleInfo;
import com.example.demo.server.model.UserInfo;


public class WriteView implements View {


    /**
     *  展示页面
     */
    public void showPage(UserInfo userInfo) {
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("+--------------------------------------------------------------------------------+");
        System.out.printf(" %s [%s]    我的博客（mylist）  主页（list）  退出登录（logout）         \n", APPUtils.now(), userInfo.getName());
        System.out.println("+--------------------------------------------------------------------------------+");
        System.out.println("|                                                                                |");
        System.out.println("|     +———————————————————————————————————————————————————————————————————+      |");
        System.out.println("|     |                      [  title:_______]                            |      |");
        System.out.println("|     |                      [summary:_______]                            |      |");
        System.out.println("|     |                                                                   |      |");
        System.out.println("|     | +——[content]————————————————————————————————————————————————————+ |      |");
        System.out.println("|     | |  ___________________________________________________________  | |      |");
        System.out.println("|     | |  ___________________________________________________________  | |      |");
        System.out.println("|     | |  ___________________________________________________________  | |      |");
        System.out.println("|     | |  ___________________________________________________________  | |      |");
        System.out.println("|     | |  ___________________________________________________________  | |      |");
        System.out.println("|     | |  ___________________________________________________________  | |      |");
        System.out.println("|     | |  ___________________________________________________________  | |      |");
        System.out.println("|     | |  ___________________________________________________________  | |      |");
        System.out.println("|     | |  ___________________________________________________________  | |      |");
        System.out.println("|     | |  ___________________________________________________________  | |      |");
        System.out.println("|     | |  ___________________________________________________________  | |      |");
        System.out.println("|     | |  ___________________________________________________________  | |      |");
        System.out.println("|     | |  ___________________________________________________________  | |      |");
        System.out.println("|     | |  ___________________________________________________________  | |      |");
        System.out.println("|     | |  ___________________________________________________________  | |      |");
        System.out.println("|     | +———————————————————————————————————————————————————————————————+ |      |");
        System.out.println("|     |                                                                   |      |");
        System.out.println("|     +———————————————————————————————————————————————————————————————————+      |");
        System.out.println("|                                                                                |");
        System.out.println("|________________________________________________________________________________|");
    }

    public void showPage(UserInfo userInfo, ArticleInfo articleInfo) {
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("+--------------------------------------------------------------------------------+");
        System.out.printf(" %s [%s]    我的博客（mylist）  主页（list）  退出登录（logout）         \n", APPUtils.now(), userInfo.getName());
        System.out.println("+————————————————————————————————————————————————————————————————————————————————+");
        System.out.println("|TITLE: " + articleInfo.getTitle());
        System.out.println("|SUMMARY: " + articleInfo.getSummary());
        System.out.println("+——[CONTENT]—————————————————————————————————————————————————————————————————————+");
        System.out.println(articleInfo.getContent());
        System.out.println("+————————————————————————————————————————————————————————————————————————————————+");
    }



    @Override
    public CommonRequest jump(CommonRequest request) {
        UserInfo userInfo = SessionUtils.getUser();
        if(userInfo == null) {
            APPUtils.printPopUp("Please Login!");
            return CommonRequest.createRequest(ViewKey.LOGIN);
        }
        ArticleInfo articleInfo = new ArticleInfo() {{
            setSummary("");
            setContent("");
            setTitle("");
            setUid(userInfo.getId());
        }};
        if(request.getBody() == null) {
            showPage(userInfo);
        }else {
            MyHashMap<String, Object> map = (MyHashMap<String, Object>) request.getBody();
            Boolean flag = (Boolean) map.get("submit");
            if(flag) {
                APPUtils.printPopUp("Submit Success!");
                return CommonRequest.createRequest(ViewKey.MY_LIST);
            }
            articleInfo = (ArticleInfo) map.get("art");
            showPage(userInfo, articleInfo);
        }
        String command = APPUtils.input();
        MyList<String> myList = APPUtils.handleCommand(command);
        if(myList.size() == 2 && myList.get(0).equals(ViewKey.WRITE)) {
            String opera = myList.get(1).toLowerCase();
            MyHashMap<String, Object> map = new MyHashMap<>();
            boolean flag = false;
            switch (opera) {
                case "title":
                    ArticleUtils.setTittle(articleInfo);
                    break;
                case "summary":
                    ArticleUtils.setSummary(articleInfo);
                    break;
                case "content":
                    ArticleUtils.setContent(articleInfo);
                    break;
                case "submit":
                    flag = ArticleUtils.submit(articleInfo);
                    break;
                default:
                    APPUtils.printPopUp("ERROR: Please Again!");
                    break;
            }
            map.put("submit", flag);
            map.put("art", articleInfo);
            return CommonRequest.createRequest(ViewKey.WRITE, map);
        }else if(myList.size() == 1) {
            String view = myList.get(0);
            CommonRequest commonRequest = APPUtils.checkJump(view,
                    ViewKey.LOGOUT, ViewKey.MY_LIST, ViewKey.LIST, ViewKey.WRITE);
            if(commonRequest != null) {
                return commonRequest;
            }
        }
        APPUtils.printPopUp("ERROR: Please Again!");
        return CommonRequest.createRequest(ViewKey.WRITE, request.getBody());
    }

}
